import os
import rtconfig
from building import *

Import('SDK_LIB')

cwd = GetCurrentDir()

# add general drivers
src = Split('''
board.c
''')

if GetDepend(['BSP_USING_SPI_FLASH']):
    src += Glob('ports/spi_flash_init.c')
    
if GetDepend(['PKG_USING_FAL']):
    src += Glob('ports/fal_flash_gd32f4_port.c')

if GetDepend(['PKG_USING_EASYFLASH']):
    src += Glob('ports/ef_sfud_port.c')

 
if GetDepend(['PKG_USING_YMODEM_OTA']):
    src += Glob('fota/rt_fota.c')
    src += Glob('fota/rt_fota_crc.c')
   
path =  [cwd]
path += [cwd + '/CubeMX_Config/Core/Inc']
path += [cwd + '/fota']

startup_path_prefix = SDK_LIB

if rtconfig.CROSS_TOOL == 'gcc':
    src += [startup_path_prefix + '/GD32F4xx_Firmware_Library/CMSIS/GD/GD32F4xx/Source/GCC/startup_gd32f4xx.s']
elif rtconfig.PLATFORM in ['armcc', 'armclang']:
    src += [startup_path_prefix + '/GD32F4xx_Firmware_Library/CMSIS/GD/GD32F4xx/Source/ARM/startup_gd32f4xx.s']
elif rtconfig.CROSS_TOOL == 'iar':
    src += [startup_path_prefix + '/GD32F4xx_Firmware_Library/CMSIS/GD/GD32F4xx/Source/IAR/startup_gd32f4xx.s']
    
CPPDEFINES = ['GD32F405']
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)

Return('group')
